package com.diagrammer.render
{
	import com.diagrammer.event.LineEvent;
	import com.diagrammer.vo.LineVO;
	
	import flash.events.MouseEvent;

	/**
	 * 线条点击事件,event.data = LineRender
	 * @eventType com.diagrammer.common.event.LineEvent
	 */
	[Event(name="lineClick", type="com.diagrammer.event.LineEvent")]
	/**
	 * 线条点击事件，同时按下ctrl键，event.data = LineRender
	 * @eventType com.diagrammer.common.event.LineEvent
	 */
	[Event(name="lineClickCtrl", type="com.diagrammer.event.LineEvent")]
	/**
	 * 线条渲染器基类，基于<code>com.diagrammer.render.BaseRender</code><br/>
	 * 
	 * @author Dwei <b>2012-02-13</b>
	 * 
	 */	
	public class LineRender extends BaseRender
	{
//============================================================================
//parameter
//============================================================================
		/**
		 * 标识vo对象是否改变
		 * @private
		 */		
		private var _voChangedFlag:Boolean;
		
		private var _vo:LineVO;
//============================================================================
//constructor
//============================================================================
		public function LineRender()
		{
			super();
			width = 0;
			height = 0;
			_voChangedFlag = false;
		}
//============================================================================
//override
//============================================================================
		override protected function commitProperties():void
		{
			if (_voChangedFlag)
			{
				commitVOProperties();
				_voChangedFlag = false;
				invalidateRender();
			}
			super.commitProperties();
		}
		
		/**
		 * @inheritDoc
		 */		
		override protected function initializeHandlers():void
		{
			super.initializeHandlers();
			initializeMouseHandler();
		}
		
		/**
		 * @inheritDoc
		 */		
		override protected function commitFocusProperties():void
		{
			
		}
		
		/**
		 * @inheritDoc
		 */		
		override protected function updateRender(unscaledWidth:Number, unscaledHeight:Number):void
		{
			
		}
//============================================================================
//protected
//============================================================================
		/**
		 * 在当前属性vo对象改变时调用，仅需要更新会影响界面显示效果的相关属性值，不必在该处进行重绘或者其它操作。此方法是一种高级方法，可在创建 LineRender 的子类时覆盖。<br/>
		 * 您无需直接调用此方法。在设置属性<b>vo</b>的值时，会自动调用该方法。<br/>
		 * 方法执行后会调用 invalidateRender() 方法，根据在该方法中设置的相关属性来更新界面
		 * 
		 */		
		protected function commitVOProperties():void
		{
			
		}
		
		/**
		 * 初始化鼠标事件监听
		 * 
		 */		
		protected function initializeMouseHandler():void
		{
			canvas.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown_handler);
		}
//============================================================================
//handler
//============================================================================
		protected function mouseDown_handler(event:MouseEvent):void
		{
			event.stopPropagation();
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove_stage_handler);
			canvas.addEventListener(MouseEvent.MOUSE_UP, mouseUp_handler);
		}
		
		protected function mouseUp_handler(event:MouseEvent):void
		{
			event.stopPropagation();
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove_stage_handler);
			canvas.removeEventListener(MouseEvent.MOUSE_UP, mouseUp_handler);
			if (event.ctrlKey)
				dispatchEvent(new LineEvent(LineEvent.LINE_CLICK_CTRL, this));
			else
				dispatchEvent(new LineEvent(LineEvent.LINE_CLICK, this));
		}
		
		protected function mouseMove_stage_handler(event:MouseEvent):void
		{
			event.stopPropagation();
			canvas.removeEventListener(MouseEvent.MOUSE_UP, mouseUp_handler);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove_stage_handler);
		}
//============================================================================
//getter/setter
//============================================================================

		[Bindable]
		/**
		 * 线条VO对象，不能为null
		 */
		public function get vo():LineVO
		{
			return _vo;
		}

		/**
		 * @private
		 */
		public function set vo(value:LineVO):void
		{
			if (value == vo) return;
			if (!value) throw new Error("LineRender.vo不能为null");
			_vo = value;
			_voChangedFlag = true;
			invalidateProperties();
		}


	}
}